package com.example.comsys.util;

import com.example.comsys.model.Result;

/**
 * TODO
 *
 * @author wjh
 * @date 2020/1/1, 周三
 */
public class ResultUtils {

  /**
   * 返回失败模型
   *
   * @param msg 消息
   * @return Result
   */
  public static <D> Result<D> newFailedResult(String msg, String code) {
    Result<D> r = new Result<>();
    r.setSuccess(false);
    r.setMessage(msg);
    r.setCode(code);
    return r;
  }

  /**
   * 返回失败模型
   *
   * @param msg 消息
   * @return Result
   */
  public static <D> Result<D> newFailedResult(String msg) {
    Result<D> r = new Result<>();
    r.setSuccess(false);
    r.setMessage(msg);
    return r;
  }

  /**
   * 返回失败模型
   *
   * @param msg 消息
   * @return Result
   */
  public static <D> Result<D> newFailedResult(String msg, String code, D data) {
    Result<D> r = newFailedResult(msg, code);
    r.setData(data);
    return r;
  }

  /**
   * 返回失败模型
   *
   * @param msg 消息
   * @return Result
   */
  public static <D> Result<D> newFailedResult(String msg, D data) {
    Result<D> r = newFailedResult(msg);
    r.setData(data);
    return r;
  }


  /**
   * 返回成功模型
   *
   * @param msg 消息
   * @param data 数据
   * @return Result
   */
  public static <D> Result<D> newSuccessfulResult(String msg, D data) {
    Result<D> r = new Result<>();
    r.setSuccess(true);
    r.setMessage(msg);
    if (data != null) {
      r.setData(data);
    }
    return r;
  }

  /**
   * 返回成功模型
   *
   * @param data 数据
   * @return Result
   */
  public static <D> Result<D> newSuccessfulResult(D data) {
    return newSuccessfulResult("success", data);
  }

  /**
   * 返回成功模型
   *
   * @return Result
   */
  public static <D> Result<D> newSuccessfulResult() {
    return newSuccessfulResult(null);
  }
}
